import com.speljohan.rsbot.script.Random;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSInterfaceChild;

public class BankPasswords extends Random {

	@Override
	public boolean activateCondition() {
		return RSInterface.getInterface(13).isValid()
				|| RSInterface.getInterface(14).isValid();
	}

	public boolean enterCode(final String pass) {
		if (!RSInterface.getInterface(13).isValid()) {
			return true;
		}
		final RSInterface pif = RSInterface.getInterface(13);
		final RSInterfaceChild[] children = pif.getChildren();
		int state = 0;
		for (int i = 21; i < 25; i++) {
			if (children[i].containsText("?")) {
				state++;
			}
		}
		state = 4 - state;

		for (int j = 11; j < 21; j++) {
			if (children[j].containsText(pass.substring(state, state + 1))) {
				atInterface(children[j - 10]);
				wait(random(1000, 2000));
				break;
			} else {
				if (j == 20) {
					moveMouse(random(0, 750), random(0, 500));
				}
			}
		}

		if (state == 3 || !RSInterface.getInterface(13).isValid()) {
			return true;
		}
		return false;
	}

	@Override
	public String getAuthor() {
		return "Holo";
	}

	@Override
	public String getName() {
		return "Bank passwords manager";
	}

	@Override
	public double getVersion() {
		return 1;
	}

	@Override
	public int loop() {
		if (RSInterface.getInterface(14).isValid()) {
			atInterface(RSInterface.getInterface(14).getChild(1));
			wait(random(800, 1500));
			return -1;
		}

		final String pin = getAccountProperty("pin");
		if (pin == null || pin == "") {
			log.warning("You must add 'Pin' key in account manager");
			wait(20000);
			return -1;
		} else {
			log.info("Entering pin code");
		}

		while (!enterCode(pin)) {
			// wait(random(300,1000));
		}

		// Click to continue
		if (RSInterface.getInterface(211).isValid()) {
			atInterface(RSInterface.getInterface(211).getChild(3));
			return random(100, 1500);
		}

		return -1;
	}
}
